﻿using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using CommonEntity.CommonImageEntities;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using ScienceAIApp.ViewModels.ToolBarPanel;
using ScienceAppService.Entities;
using ScienceAppService.ModuleInterface;

namespace ScienceAIApp.ViewModels;
public class AiToolBarViewModel : ScienceBaseViewModel
{
    private bool _noFrush;
    private IImageShowService _imageShowService;
    private IMarkDrawService _markDrawService;
    private MarkListViewModel _markListViewModel;
    private CrossLineViewModel _crossLineViewModel;
    private DrawBrushViewModel _drawBrushViewModel;

    [Reactive] public int WindowMax { get; set; }
    [Reactive] public int WindowMin { get; set; }
    [Reactive] public int StartValue { get; set; }
    [Reactive] public int EndValue { get; set; }

    [Reactive] public bool IsSelectCrossLine { get; set; }
    [Reactive] public bool IsSelectDrawLine { get; set; }
    [Reactive] public bool IsSelectEraseLine { get; set; }
    [Reactive] public bool IsSelectMarkLine { get; set; }

    [Reactive] public ScienceBaseViewModel SubToolViewModel { get; set; }

    public ReactiveCommand<SegToolsEnum, Unit> SwitchToolBarCommand { get; set; }


    public AiToolBarViewModel(IImageShowService imageShowService, IMarkDrawService markDrawService, MarkListViewModel markListViewModel, 
        CrossLineViewModel crossLineViewModel, DrawBrushViewModel drawBrushViewModel)
    {
        _imageShowService = imageShowService;
        _markDrawService = markDrawService;

        _markListViewModel = markListViewModel;
        _crossLineViewModel = crossLineViewModel;
        _drawBrushViewModel = drawBrushViewModel;

        _imageShowService.FrushImageInfoEvent += FrushWindowAdjustHandler;

        // 调节窗宽窗位
        this.WhenAnyValue(x => x.StartValue, x => x.EndValue,
            (s, e) => new Tuple<int, int>(s, e)).Do(async t =>
        {
            if (t.Item1 >= t.Item2 || _noFrush) return;
            await _imageShowService.AdjustWindow(t.Item1, t.Item2);
        }).Subscribe();

        // 菜单切换 十字线   绘制   擦除   标记点
        SwitchToolBarCommand = ReactiveCommand.CreateFromTask<SegToolsEnum>(async en =>
        {
            IsSelectCrossLine = false;
            IsSelectDrawLine = false;
            IsSelectEraseLine = false;
            IsSelectMarkLine = false;

            // 非十字线 点击消失
            if (en != SegToolsEnum.CrossLine)
            {
                await _imageShowService.FrushCrossLine(new int[] { -1, -1, -1 });
            }

            if (AppServiceEnv.ImageInfo == null) return;

            switch (en)
            {
                case SegToolsEnum.CrossLine:
                    SubToolViewModel = _crossLineViewModel;
                    IsSelectCrossLine = true;
                    await _imageShowService.FrushCrossLine(new int[] { AppServiceEnv.ImageInfo.Width / 2, AppServiceEnv.ImageInfo.Height / 2,
                    AppServiceEnv.ImageInfo.Count / 2});
                    _markDrawService.SetMarkOrDrawType(en);
                    break;
                case SegToolsEnum.DrawNone:
                    SubToolViewModel = _drawBrushViewModel;
                    IsSelectDrawLine = true;
                    _drawBrushViewModel.IsShowDrawPanel = true;
                    _markDrawService.SetMarkOrDrawType(_drawBrushViewModel.CurrenToolsEnum);
                    break;
                case SegToolsEnum.Erase:
                    SubToolViewModel = _drawBrushViewModel;
                    IsSelectEraseLine = true;
                    _drawBrushViewModel.IsShowDrawPanel = false;
                    _markDrawService.SetMarkOrDrawType(en);
                    break;
                case SegToolsEnum.MarkNone:
                    SubToolViewModel = _markListViewModel;
                    IsSelectMarkLine = true;
                    _markDrawService.SetMarkOrDrawType(_markListViewModel.CurrenToolsEnum);
                    break;
            }

        });


        // 默认选项
        SubToolViewModel = new ScienceBaseViewModel();
    }

    private void FrushWindowAdjustHandler(ImageInfo info)
    {
        RxApp.MainThreadScheduler.Schedule(async () =>
        {
            _noFrush = true;
            WindowMax = info.OriginWindowMax;
            WindowMin = info.OriginWindowMin;
            await Task.Delay(100);
            EndValue = info.WinEnd;
            StartValue = info.WinStart;
            await Task.Delay(100);
            _noFrush = false;
        });
        
    }
}
